from PySide2.QtCore import Qt

from PySide2.QtWidgets import(
    QMainWindow, QMessageBox,
    QDataWidgetMapper, QAbstractItemView,
    QItemDelegate, QPushButton, QHBoxLayout, QWidget
)

from .ui_main_win import Ui_MainWindow

from .test_model import TableModel


import time


class MyButtonDelegate(QItemDelegate):
    def __init__(self, parent=None, window=None):
        super(MyButtonDelegate, self).__init__(parent)
        self.window = window

    def paint(self, painter, option, index):
        if not self.parent().indexWidget(index):
            button_read = QPushButton(
                self.tr('查看'),
                self.parent(),
                clicked=self.window.cellButtonClicked
                # clicked=self.parent().cellButtonClicked
            )
            # button_write = QPushButton(
            #     self.tr('写'),
            #     self.parent(),
            #     clicked=self.window.cellButtonClicked
            #     # clicked=self.parent().cellButtonClicked
            # )

            button_read.index = [index.row(), index.column()]
            # button_write.index = [index.row(), index.column()]
            h_box_layout = QHBoxLayout()
            h_box_layout.addWidget(button_read)
            # h_box_layout.addWidget(button_write)
            h_box_layout.setContentsMargins(0, 0, 0, 0)
            h_box_layout.setAlignment(Qt.AlignCenter)
            widget = QWidget()
            widget.setLayout(h_box_layout)
            self.parent().setIndexWidget(
                index,
                widget
            )


class DataWidgetMapper(QDataWidgetMapper):
    def __init__(self, parent=None):
        super().__init__(parent)

    def currentIndexChanged(self, index):
        print('DataWidgetMapper',  index)
        return super().currentIndexChanged(index)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.after_setupUi()

        model = TableModel(self.ui.table)

        self.ui.table.setModel(model)
        self.ui.table.setSelectionMode(QAbstractItemView.SingleSelection)

        mapper = DataWidgetMapper(self)
        mapper.setModel(model)
        mapper.addMapping(self.ui.nameEdit, 1)
        mapper.addMapping(self.ui.emailEdit, 2)
        mapper.addMapping(self.ui.nameLabel, 1)
        mapper.addMapping(self.ui.emailLabel, 2)

        selection_model = self.ui.table.selectionModel()
        selection_model.currentRowChanged.connect(mapper.setCurrentModelIndex)
        self.ui.table.setCurrentIndex(model.index(0, 0))

        self.ui.table.setItemDelegateForColumn(
            0, MyButtonDelegate(self.ui.table, self))

    def after_setupUi(self):
        self.ui.actionQuit.triggered.connect(self.close)
        self.ui.actionQuit.setShortcut("Ctrl+Q")
        # self.ui.actionQuit.setShortcut(QKeySequence.Quit)

        self.ui.actionAbout.triggered.connect(self.showAbout)
        self.ui.menubar.setNativeMenuBar(False)

    def cellButtonClicked(self):
        print("Cell Button Clicked", self.sender().index)

    def showAbout(self):
        # msgBox = QMessageBox()
        # msgBox.setText('欢迎使用 软件. ')
        # msgBox.setWindowTitle('关于')
        # msgBox.exec()

        QMessageBox.about(self, self.tr("About"),
                          self.tr("<p>The <b>App</b> example shows how to use odoo "
                                  "with pyside2."))
